import { login } from '@/api/user.js'
export default {
  namespaced: true,
  state: {
    profile: {
      id: '',
      avatar: '',
      nickname: '',
      account: '',
      mobile: '',
      token: ''
    }
  },
  mutations: {
    login(state, data) {
      state.profile = data
    },
    logout(state) {
      state.profile = {}
    }
  },
  actions: {
    async asyncLogin({ commit }, obj) {
      try {
        const res = await login(obj)
        commit('login', res)
        console.log(res)
        alert('登录成功')
      } catch (err) {
        console.log(err.response)
        alert('登录失败')
      }
    },
    asyncLogout({ commit }) {
      commit('logout')
      commit('cart/getCartList', [], { root: true }) // 跨模块调用
    }
  }
}
